<template>
  <div ref="scrollContainer" class="scroll-container" @wheel.prevent="handleScroll">
    <div ref="scrollWrapper" :style="{left: left + 'px'}" class="scroll-wrapper">
      <slot/>
    </div>
  </div>
</template>

<script>
const padding = 15
export default {
  name: 'ScrollPane',
  data () {
    return {
      left: 0
    }
  },
  methods: {
    moveToTarget ($target) {
      const $container = this.$refs.scrollContainer
      const $containerWidth = $container.offsetWidth
      const $targetLeft = $target.offsetLeft
      const $targetWidth = $target.offsetWidth

      if ($targetLeft < -this.left) {
        // 当前点击需要显示的 tag相对scrollContainer左边溢出(被隐藏), 显示时需要右移scrollWrapper
        this.left = -$targetLeft + padding
      } else if ($targetLeft + padding > -this.left && $targetLeft + $targetWidth < -this.left + $containerWidth - padding) {
        // 当前点击需要显示或新增加的tag可以放在scrollContainer容器中，不需要操作
      } else {
        // 当前点击需要显示或新增加的tag相对scrollContainer右边溢出(被隐藏), 显示时需要左移scrollContainer
        this.left = -($targetLeft - ($containerWidth - $targetWidth) + padding)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .scroll-container {
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    width: 100%;
    .scroll-wrapper {
        position: absolute;
    }
  }
</style>
